/*
 * Copyright (c) csii.com.cn 2016 zhaojin
 */

package com.csii.ssm.controller.tianhong;

import com.csii.ssm.common.Dict;
import com.csii.ssm.core.CommonDict;
import com.csii.ssm.dto.AjaxData;
import com.csii.ssm.exception.AccessRuntimeException;
import com.csii.ssm.exception.AccessRuntimeExceptionDict;
import com.csii.ssm.service.IPjnlService;
import com.csii.ssm.service.IUserService;
import com.csii.ssm.util.SvcServiceFactory;
import com.csii.ssm.virwebservice.SvcServiceStub;
import com.csii.ssm.virwebservice.SvcServiceStub.SvcInfo;
import com.csii.ssm.virwebservice.SvcServiceStub.TransXResponse;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ServletContextAware;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 天虹交易密码/验证码管理类控制器
 *
 * @author Charis
 * @create 2016-08-12-08:59
 */
@Controller
public class PwdController extends ThBaseController implements ServletContextAware {

    private static final Logger logger = LoggerFactory.getLogger(PwdController.class);

    @Inject
    private IUserService userService;

    @Inject
    private IPjnlService pjnlService;

    /**
     * 请求短信验证码
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_GETSMSTOKEN, method = RequestMethod.GET)
    public void getSmsToken( Model model, HttpSession httpSession) {
        SvcServiceStub s = null;
        try {
            s = SvcServiceFactory.getInstance();

            SvcInfo svc = getComSvcInfo(Dict.TRS_GETSMSTOKEN);
            svc.setAccno("32318493214");
            svc.setSystrace("432543");

            TransXResponse rs = s.transX(SvcServiceFactory.packageSvcInfoToTransX(svc));
            SvcInfo rsvc = rs.getOut();

            AjaxData ajaxData = getAjaxData();
            ajaxData.put("rsvc",rsvc);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AxisFault e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_VIR_WEBSERVICE_CONNECT_FAIL);
        } catch (RemoteException e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_VIR_WEBSERVICE_CONNECT_FAIL);
        }catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_GETACCESSSECRETKEY);
        }

        /*try {
            Map smsData = new HashMap();
            smsData.put("txn_id",headFormatter.getTxnMaping().get(Dict.TRS_GETSMSTOKEN));
            smsData.put("chnl","161");
            smsData.put("accno","161111");
            smsData.put("systrace","161123");
            smsData.put("rc","161122");
            smsData.put("rc_detail","返回码解释:请求短信验证码");
            smsData.put("acq_id","012345");
            smsData.put("org_code","12345");
            smsData.put("status","1");
            smsData.put("teller","code123");
            smsData.put("txn_date","2016-8-29");
            AjaxData ajaxData = getAjaxData();
            ajaxData.setDataMap(smsData);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_GETACCESSSECRETKEY);
        }*/
    }
    /**
     * 交易密码修改响应
     * @param pin_data 原密码
     * @param new_pin 新密码
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_MODIFYTRSPWDCONFIRM, method = RequestMethod.POST)
    public void modifyTrsPwdConfirm(@RequestParam(required=true) String resToken,@RequestParam String pin_data,@RequestParam String new_pin, Model model, HttpSession httpSession) {
        try {
            //todo  	交易密码修改
            // User user = (User) httpSession.getAttribute(Dict.LOGINUSER);
            //logger.debug(user.getTel());
            AjaxData ajaxData = getAjaxData();
            Map data = new HashMap();
            data.put("txn_id",headFormatter.getTxnMaping().get(Dict.TRS_MODIFYTRSPWDCONFIRM));
            data.put("chnl","158");
            data.put("accno","158111");
            data.put("pin_retry","3");
            data.put("systrace","158123");
            data.put("rc","158122");
            data.put("rc_detail","返回码解释:交易密码修改");
            data.put("acq_id","012345");
            data.put("org_code","12345");
            data.put("teller","code123");
            data.put("txn_date","2016-8-30");
            data.put("txn_time",new Date());
            ajaxData.setDataMap(data);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_MODIFYTRSPWDCONFIRM);
        }
    }

    /**
     * 重置/设置密码响应
     * @param pin_data 新支付密码
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_RESETTRSPWDCONFIRM, method = RequestMethod.POST)
    public void resetTrsPwdConfirm(@RequestParam(required=true) String resToken,@RequestParam String pin_data,Model model, HttpSession httpSession) {
        try {
            //todo  	重置/设置密码
            // User user = (User) httpSession.getAttribute(Dict.LOGINUSER);
            //logger.debug(user.getTel());
            AjaxData ajaxData = getAjaxData();
            Map data = new HashMap();
            data.put("txn_id",headFormatter.getTxnMaping().get(Dict.TRS_RESETTRSPWDCONFIRM));
            data.put("chnl","159");
            data.put("accno","159111");
            data.put("pin_mode","3");
            data.put("systrace","159123");
            data.put("rc","159122");
            data.put("rc_detail","返回码解释:重置/设置密码");
            data.put("acq_id","012345");
            data.put("org_code","12345");
            data.put("teller","code123");
            data.put("txn_date","2016-8-30");
            data.put("txn_time",new Date());
            ajaxData.setDataMap(data);
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_RESETTRSPWDCONFIRM);
        }
    }
    /**
     * 响应交易密码验证
     * @param pin_data  支付密码
     * @param model
     * @param httpSession
     */
    @RequestMapping(value = Dict.TRS_TRSPWDVALIDATE, method = RequestMethod.POST)
    public void trsPwdValidate(@RequestParam(required=true) String resToken,@RequestParam String pin_data,Model model, HttpSession httpSession) {
        try {
            //todo  	交易密码验证
            // User user = (User) httpSession.getAttribute(Dict.LOGINUSER);
            //logger.debug(user.getTel());
            AjaxData ajaxData = getAjaxData();
            Map data = new HashMap();
            data.put("txn_id",headFormatter.getTxnMaping().get(Dict.TRS_TRSPWDVALIDATE));
            data.put("chnl","160");
            data.put("accno","160111");
            data.put("pin_retry","3");
            data.put("systrace","160123");
            data.put("rc","160122");
            data.put("rc_detail","返回码解释:交易密码验证");
            data.put("acq_id","012345");
            data.put("org_code","12345");
            data.put("teller","code123");
            data.put("txn_date","2016-8-30");
            data.put("txn_time",new Date());
            model.addAttribute(CommonDict.RES,ajaxData);
        } catch (AccessRuntimeException e) {
            throw new AccessRuntimeException(e.getMessage(), e);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AccessRuntimeException(AccessRuntimeExceptionDict.ERROR_TRSPWDVALIDATE);
        }
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}


